import csv
import pandas as pd

# 模拟从年报中提取的财务数据
financial_data = {
    "Year": ["2023", "2022"],
    "Sales Revenue (RMB)": [704174, 642338],
    "Operating Profit (RMB)": [104401, 42216],
    "Net Profit (RMB)": [86950, 35562],
    "R&D Expenses (RMB)": [164721, 161494],
    "Total Assets (RMB)": [1263597, 1063804],
    "Total Liabilities (RMB)": [756029, 626728],
    "Owner's Equity (RMB)": [507568, 437076]
}

# 将数据转换为DataFrame
df = pd.DataFrame(financial_data)

# 将DataFrame存储到CSV文件
csv_file_path = "financial_data.csv"
df.to_csv(csv_file_path, index=False, encoding='utf-8-sig')

print(f"Financial data has been saved to {csv_file_path}")
import pandas as pd

# 读取CSV文件
file_path = 'financial_data.csv'
# 由于数据格式问题，我们需要先将数据转换为正确的格式
data = {
    "Year": [2023, 2022],
    "Sales Revenue (RMB)": [704174, 642338],
    "Operating Profit (RMB)": [104401, 42216],
    "Net Profit (RMB)": [86950, 35562],
    "R&D Expenses (RMB)": [164721, 161494],
    "Total Assets (RMB)": [1263597, 1063804],
    "Total Liabilities (RMB)": [756029, 626728],
    "Owner's Equity (RMB)": [507568, 437076]
}
df = pd.DataFrame(data)

# 计算财务指标
# ROE (Return on Equity) = Net Profit / Owner's Equity
df['ROE'] = df['Net Profit (RMB)'] / df['Owner\'s Equity (RMB)']

# ROA (Return on Assets) = Net Profit / Total Assets
df['ROA'] = df['Net Profit (RMB)'] / df['Total Assets (RMB)']

# 资产负债率 = Total Liabilities / Total Assets
df['Debt to Asset Ratio'] = df['Total Liabilities (RMB)'] / df['Total Assets (RMB)']

# 净利润率 = Net Profit / Sales Revenue
df['Net Profit Margin'] = df['Net Profit (RMB)'] / df['Sales Revenue (RMB)']

# 营业利润率 = Operating Profit / Sales Revenue
df['Operating Profit Margin'] = df['Operating Profit (RMB)'] / df['Sales Revenue (RMB)']

# 研发投入占比 = R&D Expenses / Sales Revenue
df['R&D to Sales Ratio'] = df['R&D Expenses (RMB)'] / df['Sales Revenue (RMB)']

# 资产周转率 = Sales Revenue / Total Assets
df['Asset Turnover'] = df['Sales Revenue (RMB)'] / df['Total Assets (RMB)']

# 打印结果
print(df[['ROE', 'ROA', 'Debt to Asset Ratio', 'Net Profit Margin', 'Operating Profit Margin', 'R&D to Sales Ratio', 'Asset Turnover']])

# 可以选择将结果保存到新的CSV文件
output_file_path = 'financial_analysis_results.csv'
df[['ROE', 'ROA', 'Debt to Asset Ratio', 'Net Profit Margin', 'Operating Profit Margin', 'R&D to Sales Ratio', 'Asset Turnover']].to_csv(output_file_path, index=False)
print(f"Analysis results have been saved to {output_file_path}")
import matplotlib.pyplot as plt

# 模拟的财务分析结果数据
years = [2023, 2022]
roe = [17.130709579800147, 8.136342420997721]  # ROE值
roa = [6.881149607034522, 3.342909032114939]  # ROA值
debt_to_asset_ratio = [59.83149690922026, 58.91386007196815]  # 资产负债率
net_profit_margin = [12.347800401605285, 5.536337566826188]  # 净利润率
operating_profit_margin = [14.826023113605444, 6.572240782890006]  # 营业利润率
r_and_d_to_sales_ratio = [23.392087751038808, 25.141592121281942]  # 研发投入占比
asset_turnover = [55.72773597911359, 60.381235641151946]  # 资产周转率

# 创建图表
plt.figure(figsize=(14, 8))

# ROE对比
plt.subplot(2, 4, 1)
plt.bar(years, roe, color='blue')
plt.xlabel('Year')
plt.ylabel('ROE (%)')
plt.title('ROE Comparison')
plt.xticks(years, rotation=45)

# ROA对比
plt.subplot(2, 4, 2)
plt.bar(years, roa, color='green')
plt.xlabel('Year')
plt.ylabel('ROA (%)')
plt.title('ROA Comparison')
plt.xticks(years, rotation=45)

# 资产负债率对比
plt.subplot(2, 4, 3)
plt.bar(years, debt_to_asset_ratio, color='red')
plt.xlabel('Year')
plt.ylabel('Debt to Asset Ratio (%)')
plt.title('Debt to Asset Ratio Comparison')
plt.xticks(years, rotation=45)

# 净利润率对比
plt.subplot(2, 4, 4)
plt.bar(years, net_profit_margin, color='purple')
plt.xlabel('Year')
plt.ylabel('Net Profit Margin (%)')
plt.title('Net Profit Margin Comparison')
plt.xticks(years, rotation=45)

# 营业利润率对比
plt.subplot(2, 4, 5)
plt.bar(years, operating_profit_margin, color='orange')
plt.xlabel('Year')
plt.ylabel('Operating Profit Margin (%)')
plt.title('Operating Profit Margin Comparison')
plt.xticks(years, rotation=45)

# 研发投入占比对比
plt.subplot(2, 4, 6)
plt.bar(years, r_and_d_to_sales_ratio, color='brown')
plt.xlabel('Year')
plt.ylabel('R&D to Sales Ratio (%)')
plt.title('R&D to Sales Ratio Comparison')
plt.xticks(years, rotation=45)

# 资产周转率对比
plt.subplot(2, 4, 7)
plt.bar(years, asset_turnover, color='pink')
plt.xlabel('Year')
plt.ylabel('Asset Turnover')
plt.title('Asset Turnover Comparison')
plt.xticks(years, rotation=45)

plt.tight_layout()
plt.show()